unit console;
 
interface

var
   CursorPosX: Integer = 0;
   CursorPosY: Integer = 0;
   CommandBuffer: String = '';
type
   TScreenColour = (scBlack,scBlue,scGreen,scCyan,scRed,scMagenta,scBrown,
                    scLightGrey,scDarkGrey,scLightBlue,scLightGreen,
                    scLightCyan,scLightRed,scLightMagenta,scLightBrown,
                    scWhite);

Procedure ClearScreen;
Procedure GoToXY(const X,Y: DWORD);

procedure kwritechr(c: Char);
procedure kwritestr(s: PChar);
procedure kwriteint(i: Integer);
//procedure kwritedword(i: DWORD);

Procedure SetTextColour(Const bgColour,fgColour: TScreenColour);
Procedure Init;

implementation

const
  clBlack  = 0;
  clBlue   = 1;
  clGreen  = 2;

Const
  OS_LOGO = 'Ultim8 OS 0.02 2014';
 
var
  vidmem: PChar = PChar($B8000);
  Attrib: Word = $0F;

Procedure ClearScreen(); [public, alias: 'ClearScreen'];
var
  i: Integer;
begin
  for i := 0 to 3999 do 
    vidmem[i] := #0;
  CursorPosX := 0;
  CursorPosY := 0;
//  FlashCursor;
end;
 
Procedure GoToXY(const X,Y: DWORD);
begin
  if X < 80 then
    CursorPosX := X;
  if Y < 24 then 
    CursorPosY := Y;
end;

procedure kwritechr(c: Char); [public, alias: 'kwritechr'];
var
        offset: Integer;
begin
        if (CursorPosY > 24) then
                CursorPosY := 0;
 
        if (CursorPosX > 79) then
                CursorPosX := 0;
 
        offset := (CursorPosX shl 1) + (CursorPosY * 160);
        vidmem[offset] := c;
        offset += 1;
        vidmem[offset] := #7;
        offset += 1;
 
        CursorPosX := (offset mod 160);
        CursorPosY := (offset - CursorPosX) div 160;
        CursorPosX := CursorPosX shr 1;
end;
 
procedure kwritestr(s: PChar); [public, alias: 'kwritestr'];
var
        offset, i: Integer;
begin
        if (CursorPosY > 24) then
                CursorPosY := 0;
 
        if (CursorPosX > 79) then
                CursorPosX := 0;
 
        offset := (CursorPosX shl 1) + (CursorPosY * 160);
       i := 0;
 
        while (s[i] <> Char($0)) do
        begin
                vidmem[offset] := s[i];
                offset += 1;
                vidmem[offset] := #7;
                offset += 1;
                i += 1;
        end;
 
        CursorPosX := (offset mod 160);
        CursorPosY := (offset - CursorPosX) div 160;
        CursorPosX := CursorPosX shr 1;
end;
 
procedure kwriteint(i: Integer); [public, alias: 'kwriteint'];
var
        buffer: array [0..11] of Char;
        str: PChar;
        digit: DWORD;
        minus: Boolean;
begin
        str := @buffer[11];
        str^ := #0;
 
        if (i < 0) then
        begin
                digit := -i;
                minus := True;
        end
        else
        begin
                digit := i;
                minus := False;
        end;
 
        repeat
                Dec(str);
                str^ := Char((digit mod 10) + Byte('0'));
                digit := digit div 10;
        until (digit = 0);
 
        if (minus) then
        begin
                Dec(str);
                str^ := '-';
        end;
 
        kwritestr(str);
end;
 
//procedure kwritedword(i: DWORD); [public, alias: 'kwritedword'];
//var
//        buffer: array [0..11] of Char;
//        str: PChar;
//        digit: DWORD;
//begin
//        for digit := 0 to 10 do
//                buffer[digit] := '0';
 
//        str := @buffer[11];
//        str^ := #0;
 
//        digit := i;
//        repeat
//                Dec(str);
//                str^ := Char((digit mod 10) + Byte('0'));
//                digit := digit div 10;
//        until (digit = 0);
 
//        kwritestr(@Buffer[0]);
//end;

Procedure SetTextColour(Const bgColour, fgColour: TScreenColour); [public, alias: 'SetTextColour'];
begin
  Attrib := (Ord(bgColour) shl 4) or (Ord(fgColour) and $0F);
end;
 
Procedure Init;
var
 i: Byte;
begin
  SetTextColour(scBlack, scGreen);
  ClearScreen;
  SetTextColour(scBlack, scGreen);
  for i := 1 to Length(OS_LOGO) do 
  begin
    VidMem[2 * (i-1) + 3960] := OS_LOGO[i];
    VidMem[2 * (i-1) + 3961] := Char(10 and $0f);
  end;
  SetTextColour(scBlack, scGreen);
  

end;

end.
